import java.io.Serializable;
import java.util.regex.Pattern;
import java.util.regex.Matcher;

public class Validation extends Object implements Serializable {
	private int currentSql = 0;
	private String [] input;
	
	public Validation(String []input1){
		input=input1;
	}
	
	public int getSQL() {
		return currentSql;
	}

	
	public boolean validate() {
		boolean result = false;
		int inputLength = input.length;
		if (inputLength > 3) {
			currentSql=0;
			return false;
		} else if (inputLength == 1) {

			Matcher m;
			// DELETE ALL
			Pattern firstLine = Pattern
					.compile("\\p{Blank}*DELETE\\p{Blank}+[^\\p{Blank}]+\\p{Blank}*");
			m = firstLine.matcher(input[0]);
			boolean flag1 = m.matches();
			if (flag1) {
				currentSql = 2;
				return true;
			}
		} else if (inputLength == 2) {
			Matcher m;
			Pattern firstLine;
			Pattern secondLine;
			boolean flag1 = false, flag2 = false;
			// INSERT
			// ========================
			firstLine = Pattern
					.compile("\\p{Blank}*INSERT\\p{Blank}+INTO\\p{Blank}+[^\\p{Blank}]+\\p{Blank}*\\(\\p{Blank}*([^\\p{Blank},]+\\p{Blank}*\\,\\p{Blank}*)*[^\\p{Blank}]+\\p{Blank}*\\)\\p{Blank}*");
			m = firstLine.matcher(input[0]);
			flag1 = m.matches();
			secondLine = Pattern
					.compile("\\p{Blank}*VALUES\\p{Blank}*\\(\\p{Blank}*([^\\p{Blank},]+\\p{Blank}*\\,\\p{Blank}*)*[^\\p{Blank}]+\\p{Blank}*\\)\\p{Blank}*");
			m = secondLine.matcher(input[1]);
			flag2 = m.matches();
			if (flag1 && flag2) {
				currentSql = 1;
				return true;
			}
			// DELETE
			// ========================
			firstLine = Pattern
					.compile("\\p{Blank}*DELETE\\p{Blank}+[^\\p{Blank}]+\\p{Blank}*");
			m = firstLine.matcher(input[0]);
			flag1 = m.matches();
			secondLine = Pattern
					.compile("\\p{Blank}*WHERE\\p{Blank}+[^\\p{Blank}]+\\p{Blank}*(=|>|<|(<>)|(>=)|(<=))\\p{Blank}*[^\\p{Blank}]+\\p{Blank}*");
			m = secondLine.matcher(input[1]);
			flag2 = m.matches();
			if (flag1 && flag2) {
				currentSql = 2;
				return true;
			}
			// UPDATE ALL 
			// ========================
			firstLine = Pattern
					.compile("\\p{Blank}*UPDATE\\p{Blank}+[^\\p{Blank}]+\\p{Blank}*");
			m = firstLine.matcher(input[0]);
			flag1 = m.matches();
			secondLine = Pattern
					.compile("\\p{Blank}*SET\\p{Blank}+([^\\p{Blank},]+\\p{Blank}*(=)\\p{Blank}*[^\\p{Blank},]+\\p{Blank}*,)*\\p{Blank}*[^\\p{Blank},]+\\p{Blank}*(=)\\p{Blank}*[^\\p{Blank},]+\\p{Blank}*");
			m = secondLine.matcher(input[1]);
			flag2 = m.matches();
			if (flag1 && flag2) {
				currentSql = 3;
				return true;
			}
			// SELECT ALL without condition
			// ========================
			firstLine = Pattern
					.compile("\\p{Blank}*SELECT\\p{Blank}+\\*\\p{Blank}*");
			m = firstLine.matcher(input[0]);
			flag1 = m.matches();
			secondLine = Pattern
					.compile("\\p{Blank}*FROM\\p{Blank}+[^\\p{Blank}]+\\p{Blank}*");
			m = secondLine.matcher(input[1]);
			flag2 = m.matches();
			if (flag1 && flag2) {
				currentSql = 4;
				return true;
			}
			// SELECT without condition
			// ========================
			firstLine = Pattern
					.compile("\\p{Blank}*SELECT\\p{Blank}*\\(\\p{Blank}*([^\\p{Blank},]+\\p{Blank}*\\,\\p{Blank}*)*[^\\p{Blank}]+\\p{Blank}*\\)\\p{Blank}*");
			m = firstLine.matcher(input[0]);
			flag1 = m.matches();
			secondLine = Pattern
					.compile("\\p{Blank}*FROM\\p{Blank}+[^\\p{Blank}]+\\p{Blank}*");
			m = secondLine.matcher(input[1]);
			flag2 = m.matches();
			if (flag1 && flag2) {
				currentSql = 4;
				return true;
			}
			// ========================
		} else if (inputLength == 3) {
			Matcher m;
			Pattern firstLine;
			Pattern secondLine;
			Pattern thirdLine;
			boolean flag1 = false, flag2 = false, flag3 = false;
			// UPDATE with condition
			// ========================
			firstLine = Pattern
					.compile("\\p{Blank}*UPDATE\\p{Blank}+[^\\p{Blank}]+\\p{Blank}*");
			m = firstLine.matcher(input[0]);
			flag1 = m.matches();
			secondLine = Pattern
					.compile("\\p{Blank}*SET\\p{Blank}+([^\\p{Blank},]+\\p{Blank}*(=)\\p{Blank}*[^\\p{Blank},]+\\p{Blank}*,)*\\p{Blank}*[^\\p{Blank},]+\\p{Blank}*(=)\\p{Blank}*[^\\p{Blank},]+\\p{Blank}*");
			m = secondLine.matcher(input[1]);
			flag2 = m.matches();
			thirdLine = Pattern
					.compile("\\p{Blank}*WHERE\\p{Blank}+[^\\p{Blank}]+\\p{Blank}*(=|>|<|(<>)|(>=)|(<=))\\p{Blank}*[^\\p{Blank}]+\\p{Blank}*");
			m = thirdLine.matcher(input[2]);
			flag3 = m.matches();
			if (flag1 && flag2 && flag3) {
				currentSql = 3;
				return true;
			}
			// SELECT ALL with condition
			// ========================
			firstLine = Pattern
					.compile("\\p{Blank}*SELECT\\p{Blank}+\\*\\p{Blank}*");
			m = firstLine.matcher(input[0]);
			flag1 = m.matches();
			secondLine = Pattern
					.compile("\\p{Blank}*FROM\\p{Blank}+[^\\p{Blank}]+\\p{Blank}*");
			m = secondLine.matcher(input[1]);
			flag2 = m.matches();
			thirdLine = Pattern
					.compile("\\p{Blank}*WHERE\\p{Blank}+[^\\p{Blank}]+\\p{Blank}*(=|>|<|(<>)|(>=)|(<=))\\p{Blank}*[^\\p{Blank}]+\\p{Blank}*");
			m = thirdLine.matcher(input[2]);
			flag3 = m.matches();
			if (flag1 && flag2 && flag3) {
				currentSql = 4;
				return true;
			}
			// SELECT with condition
			// ========================
			firstLine = Pattern
					.compile("\\p{Blank}*SELECT\\p{Blank}*\\(\\p{Blank}*([^\\p{Blank},]+\\p{Blank}*\\,\\p{Blank}*)*[^\\p{Blank}]+\\p{Blank}*\\)\\p{Blank}*");
			m = firstLine.matcher(input[0]);
			flag1 = m.matches();
			secondLine = Pattern
					.compile("\\p{Blank}*FROM\\p{Blank}+[^\\p{Blank}]+\\p{Blank}*");
			m = secondLine.matcher(input[1]);
			flag2 = m.matches();
			thirdLine = Pattern
					.compile("\\p{Blank}*WHERE\\p{Blank}+[^\\p{Blank}]+\\p{Blank}*(=|>|<|(<>)|(>=)|(<=))\\p{Blank}*[^\\p{Blank}]+\\p{Blank}*");
			m = thirdLine.matcher(input[2]);
			flag3 = m.matches();
			if (flag1 && flag2 && flag3) {
				currentSql = 4;
				return true;
			}
		}
		return result;
	}
	public boolean finalParsing(){
		if(validate()){
			queryParser parse=new queryParser(input,getSQL());
			return true;
		}else{
			return false;
		}
	}
	/**
	 * insert=>1
	 * deleteAll=>2
	 * delete=>3
	 * updateAll=>4
	 * update=>5
	 * selectAll without=>6 
	 * selectAll with=>7
	 * select without=>8
	 * select with=>9
	 */
	public static void main(String[] args) {
		String[] input = {  "INSERT INTO KHJRNVJK(KLUHJN,REA;SFDKJ,REOK)",
	"    VALUES           (KJ,BSNZILK           ,            SRETTG,          YRSNGDBX      ,  GBBBBB       )   " };
		Validation m = new Validation(input);
		System.out.println(m.validate());
		System.out.println(m.getSQL());
	}
}
